//
//  ChapterCell.m
//  AssistantDriving
//
//  Created by dcr on 2017/8/28.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "ChapterCell.h"

@interface ChapterCell()
@property (nonatomic,strong) UIImageView     *nextImageView;
@property (nonatomic,strong) UIView      *dividingLineView;
@end

@implementation ChapterCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    NSString * identifier = NSStringFromClass([ChapterCell class]);
    ChapterCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ChapterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat padding = kSpace;
    
    CGFloat indexLabelX = padding;
    CGFloat indexLabelW = 60;
    CGFloat indexLabelH = indexLabelW;
    CGFloat indexLabelY = (self.height - indexLabelH) * 0.5f;
    self.indexLabel.frame = CGRectMake(indexLabelX, indexLabelY, indexLabelW, indexLabelH);
    
    CGFloat titleLabelX = indexLabelX + indexLabelW + padding;
    CGFloat titleLabelY = 0;
    CGFloat titleLabelW = (self.width - titleLabelX - padding)-15;
    CGFloat titleLabelH = self.height;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    CGFloat nextImageViewW = 10;
    CGFloat nextImageViewH = nextImageViewW;
    CGFloat nextImageViewX = self.width - nextImageViewW - padding;
    CGFloat nextImageViewY = (self.height - nextImageViewH) * 0.5f;
    self.nextImageView.frame = CGRectMake(nextImageViewX, nextImageViewY, nextImageViewW, nextImageViewH);

    CGFloat dividingLineViewX = padding;
    CGFloat dividingLineViewW = self.width - 2 * padding;
    CGFloat dividingLineViewH = 0.5f;
    CGFloat dividingLineViewY = self.height - dividingLineViewH;
    self.dividingLineView.frame = CGRectMake(dividingLineViewX, dividingLineViewY, dividingLineViewW, dividingLineViewH);
}

- (UILabel *)indexLabel {
    if (!_indexLabel) {
        _indexLabel = [UILabel new];
        _indexLabel.textColor = KC02_2c2c2c;
        _indexLabel.backgroundColor = [UIColor colorWithRed:0.41 green:0.67 blue:0.42 alpha:1.00];
        _indexLabel.textAlignment = NSTextAlignmentCenter;
        _indexLabel.layer.masksToBounds = true;
        _indexLabel.layer.cornerRadius = 15;
    }
    return _indexLabel;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = KC02_2c2c2c;
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = WeekFont3;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UIImageView *)nextImageView {
    if (!_nextImageView) {
        _nextImageView = [UIImageView new];
        _nextImageView.contentMode = UIViewContentModeScaleAspectFill;
        _nextImageView.clipsToBounds = YES;
        _nextImageView.layer.borderColor = [UIColor clearColor].CGColor;
        _nextImageView.layer.borderWidth = 0.5f;
        _nextImageView.image = [UIImage imageNamed:@"arrow_right"];
    }
    return _nextImageView;
}

- (UIView *)dividingLineView {
    if (!_dividingLineView) {
        _dividingLineView = [UIView new];
        _dividingLineView.backgroundColor = [UIColor lightGrayColor];
        _dividingLineView.alpha = 0.3f;
    }
    return _dividingLineView;
}

- (void)setupSubviews {
    [self.contentView addSubview:self.indexLabel];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.nextImageView];
    [self.contentView addSubview:self.dividingLineView];
}

+ (CGFloat)cellHeight {
    return 60;
}

@end
